<template>
	<view class="content wrap">
		<u-form :model="form" ref="uForm"  :rules="rules">
			<u-form-item label="姓名" label-width="150" left-icon="account" prop="driverName">
				<u-input v-model="form.driverName" placeholder="请输入姓名"/>
			</u-form-item>
			
			<u-form-item label="手机号" label-width="150" left-icon="phone" prop="tel">
				<u-input v-model="form.tel" type="digit" placeholder="请输入手机号/账号"/>
			</u-form-item>
			
			<u-form-item label="默认密码" label-width="150" prop="password">
				<u-input v-model="form.password" placeholder="请输入密码"/>
			</u-form-item>
			
			<u-form-item label="性别" label-width="150">
				<u-input v-model="form.six" type="select" @click="getS()"/>
			</u-form-item>
			
			<u-form-item label="车牌号码" label-width="170" left-icon="car" prop="privatecarCarnumber">
				<u-input v-model="form.privatecarCarnumber" placeholder="请输入车牌号码"/>
			</u-form-item>
			
			<u-form-item label="车牌颜色" label-width="170" left-icon="car" prop="privatecarColour">
				<u-input v-model="form.privatecarColour" type="select" @click="getR()"/>
			</u-form-item>
			
			<u-form-item label="车型" label-width="170" left-icon="car" prop="privatecarType">
				<u-input v-model="form.privatecarType" type="select" @click="getC()"/>
			</u-form-item>
			
			<u-form-item label="车长(m)" label-width="150" left-icon="car" prop="privatecarCarlong">
				<u-input v-model="form.privatecarCarlong" type="digit" placeholder="请输入车长(m)" @input="longinput"/>
			</u-form-item>
			
			<u-form-item label="车宽(m)" label-width="150" left-icon="car" prop="privatecarCarwide">
				<u-input v-model="form.privatecarCarwide" type="digit" placeholder="请输入车宽(m)" @input="wideinput"/>
			</u-form-item>
			
			<u-form-item label="车高(m)" label-width="150" left-icon="car" prop="privatecarCarhigh">
				<u-input v-model="form.privatecarCarhigh" type="digit" placeholder="请输入车高(m)" @input="highinput"/>
			</u-form-item>
			
			<u-form-item label="准载体积" label-width="170" left-icon="car" prop="privatecarSurevolume">
				<u-input v-model="form.privatecarSurevolume" type="digit" placeholder="请输入准载体积m3"/>
			</u-form-item>
		</u-form>
		<u-button @click="submit">提交</u-button>
		<view>
			<u-select v-model="show" :list="carTypelist" @confirm="confirm"></u-select>
		</view>
		<view>
			<u-select v-model="show2" :list="sixlist" @confirm="confirm2"></u-select>
		</view>
		<view>
			<u-select v-model="show3" :list="colourlist" @confirm="confirm3"></u-select>
		</view>
		<view>
			<u-toast ref="uToast" />
		</view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				form:{
					driverName:"",
					tel:"",
					password:"111111",
					six:"男",
					privatecarCarnumber:"",
					privatecarColour:"蓝",
					privatecarType:"4.2米厢车",
					operateName:"",
					operateId:"",
					privatecarCarlong:"",
					privatecarCarwide:"",
					privatecarCarhigh:"",
					privatecarSurevolume:""
					
				},
				sixlist:[
					{
						value:"男",
						label:"男"
					},
					{
						value:"女",
						label:"女"
					},
				],
				colourlist:[
					{
						value:"黄",
						label:"黄"
					},
					{
						value:"蓝",
						label:"蓝"
					},
					{
						value:"绿",
						label:"绿"
					},
				],
				carTypelist:[
					{
						value:"4.2米厢车",
						label:"4.2米厢车"
					},
					{
						value:"6.8米厢车",
						label:"6.8米厢车"
					},
					{
						value:"7.6米厢车",
						label:"7.6米厢车"
					},
					{
						value:"9.6米厢车",
						label:"9.6米厢车"
					},
					{
						value:"12.5米厢车",
						label:"12.5米厢车"
					},
					{
						value:"13.5米厢车",
						label:"13.5米厢车"
					},
					{
						value:"16.5米厢车",
						label:"16.5米厢车"
					},
					{
						value:"17.5米厢车",
						label:"17.5米厢车"
					},
					{
						value:"2.7米高栏车",
						label:"2.7米高栏车"
					},
					{
						value:"3.3米高栏车",
						label:"3.3米高栏车"
					},
					{
						value:"4.2米高栏车",
						label:"4.2米高栏车"
					},
					{
						value:"5.2米高栏车",
						label:"5.2米高栏车"
					},
					{
						value:"6.2米高栏车",
						label:"6.2米高栏车"
					},
					{
						value:"6.8米高栏车",
						label:"6.8米高栏车"
					},
					{
						value:"7.6米高栏车",
						label:"7.6米高栏车"
					},
					{
						value:"9.6米高栏车",
						label:"9.6米高栏车"
					},
					{
						value:"12.5米高栏车",
						label:"12.5米高栏车"
					},
					{
						value:"13.5米高栏车",
						label:"13.5米高栏车"
					},
					{
						value:"16.5米高栏车",
						label:"16.5米高栏车"
					},
					{
						value:"17.5米高栏车",
						label:"17.5米高栏车"
					},
					{
						value:"3.3米平板",
						label:"3.3米平板"
					},
					{
						value:"3.8米平板",
						label:"3.8米平板"
					},
					{
						value:"4.2米平板",
						label:"4.2米平板"
					},
					{
						value:"5.2米平板",
						label:"5.2米平板"
					},
					{
						value:"6.8米平板",
						label:"6.8米平板"
					},
					{
						value:"12.5米平板",
						label:"12.5米平板"
					},
					{
						value:"13.5米平板",
						label:"13.5米平板"
					},
					{
						value:"16.5米平板",
						label:"16.5米平板"
					},
					{
						value:"17.5米平板",
						label:"17.5米平板"
					},
					{
						value:"面包车",
						label:"面包车"
					},
					{
						value:"依维柯",
						label:"依维柯"
					},
				],
			    rules: {
				},
				show:false,
				show2:false,
				show3:false,
				olist:[],
			};
		},
		onLoad:function(open){
			//获取司机扫码的项目id
			try{
				var params = this.getQueryParams(open.q)
				if(typeof  params.operateId != 'undefined' && params.operateId != '' && params.operateId != null){
					this.form.operateId= params.operateId
				} 
			} catch (e){
				let getStorageSyncOperateId = uni.getStorageSync('operateId')
				if(typeof getStorageSyncOperateId != 'undefined' && getStorageSyncOperateId != '' && getStorageSyncOperateId != null){
					this.form.operateId= getStorageSyncOperateId
				} 
			}
		},
		methods:{
			getQueryParams(queryString) {
				// 微信扫码得到的内容进行了一次编码，所以官方要求需要进行decodeURIComponent一次
				queryString = decodeURIComponent(queryString)
				let params = {}
				if (queryString) {
				  let queryArray = queryString.split('?')
				  if (queryArray.length > 1) {
					let query = queryArray[1]
					let array = query.split('&')
					array.map((value) => {
					  let valueArray = value.split('=') 
					  if (valueArray.length > 1) {
						// 还需要对value进行解码（可能涉及到在value为中文字符，在赋值到二维码前进行encodeURI编码）
						Object.assign(params, { [valueArray[0]]: decodeURI(valueArray[1]) })
					  }
					})
				  }
				}
				return params
			},
			longinput(e){
				var privatecarCarwide = this.form.privatecarCarwide//宽
				var privatecarCarhigh = this.form.privatecarCarhigh//高
				if(!isNaN(e) && e != "" && parseFloat(e) > 0){
					if(!isNaN(privatecarCarwide) && privatecarCarwide != "" && parseFloat(privatecarCarwide) > 0){
						if(!isNaN(privatecarCarhigh) && privatecarCarhigh != "" && parseFloat(privatecarCarhigh) > 0){
							this.form.privatecarSurevolume = (e * privatecarCarwide * privatecarCarhigh).toFixed(2)//改变准载体积
						}
					}
				}
			},
			wideinput(e){
				var privatecarCarlong = this.form.privatecarCarlong//长
				var privatecarCarhigh = this.form.privatecarCarhigh//高
				if(!isNaN(e) && e != "" && parseFloat(e) > 0){
					if(!isNaN(privatecarCarlong) && privatecarCarlong != "" && parseFloat(privatecarCarlong) > 0){
						if(!isNaN(privatecarCarhigh) && privatecarCarhigh != "" && parseFloat(privatecarCarhigh) > 0){
							this.form.privatecarSurevolume = (e * privatecarCarlong * privatecarCarhigh).toFixed(2)//改变准载体积
						}
					}
				}
			},
			highinput(e){
				var privatecarCarlong = this.form.privatecarCarlong//长
				var privatecarCarwide = this.form.privatecarCarwide//宽
				if(!isNaN(e) && e != "" && parseFloat(e) > 0){
					if(!isNaN(privatecarCarlong) && privatecarCarlong != "" && parseFloat(privatecarCarlong) > 0){
						if(!isNaN(privatecarCarwide) && privatecarCarwide != "" && parseFloat(privatecarCarwide) > 0){
							this.form.privatecarSurevolume = (e * privatecarCarlong * privatecarCarwide).toFixed(2)//改变准载体积
						}
					}
				}
			},
			getC(){
				this.show = true
			},
			getS(){
				this.show2 = true
			},
			getR(){
				this.show3 = true
			},
			confirm(e) {
				this.form.privatecarType = e[0].value
			},
			confirm2(e) {
				this.form.six = e[0].value
			},
			confirm3(e) {
				this.form.privatecarColour = e[0].value
			},
			submit() {
				if(typeof this.form.driverName =='undefined'|| this.form.driverName == null || this.form.driverName == ''){
					uni.showToast({
						title: '请输入姓名',
						duration: 2000,
						icon:'none',
					});
					return false;
				}
				if(typeof this.form.tel =='undefined'|| this.form.tel == null || this.form.tel == ''){
					uni.showToast({
						title: '请输入手机号码',
						duration: 2000,
						icon:'none',
					});
					return false;
				}
				if(typeof this.form.password =='undefined'|| this.form.password == null || this.form.password == ''){
					uni.showToast({
						title: '请输入密码',
						duration: 2000,
						icon:'none',
					});
					return false;
				}
				if(typeof this.form.privatecarCarnumber =='undefined'|| this.form.privatecarCarnumber == null || this.form.privatecarCarnumber == ''){
					uni.showToast({
						title: '请输入车牌号',
						duration: 2000,
						icon:'none',
					});
					return false;
				}
				if(typeof this.form.privatecarSurevolume =='undefined'|| this.form.privatecarSurevolume == null || this.form.privatecarSurevolume == ''){
					uni.showToast({
						title: '请输入准载体积',
						duration: 2000,
						icon:'none',
					});
					return false;
				}
				uni.showLoading({
					title: '正在提交'
				})
				this.$http.post('/rest/driverAndCar/register',{
				},{
					params: this.form,
					//anth 是否验证，非必填， false: 不需要验证 true: 需要验证,默认为true需要验证.
					custom: {auth: false},
					header: {
						'content-type': 'application/x-www-form-urlencoded',
					}
				}).then(res => {
					uni.hideLoading()
					if(res.data.code == 200) {
						uni.showToast({
							title: '注册成功，正在登录',
							duration: 2000
						});
						this.$http.post('/rest/login',{
						},{
							params: {
								username: this.form.tel,
								password: this.form.password,
							},
							custom: {auth: false},
							header: {
								'content-type': 'application/x-www-form-urlencoded',
							}
						}).then(res => {
							let data = res.data.data
							if(res.data.code != 200) {
								uni.showToast({
									title: res.data.message,
									duration: 2000,
									icon:'none',
								});
							} else {
								try {
									uni.setStorageSync('token', data);
									setTimeout(() => {
										this.$Router.pushTab({ 
											name: 'index',
										})
									}, 1000)
								} catch (e) {
									uni.showToast({
										title: '登录失败:'+e,
										duration: 2000,
										icon:'none',
									});
								}
							}
						}).catch(err => {
							uni.showToast({
								title: err.data.message,
								duration: 2000,
								icon:'none',
							});
						});
					} else {
						uni.showToast({
							title: res.data.message,
							duration: 2000,
							icon:'none',
						});
					}
				}).catch(err => {
					uni.showToast({
						title: err.data.message,
						duration: 2000,
						icon:'none',
					});
				});
			},
		}
	}
</script>

<style lang="scss">
	page{
			background-color: #ffffff;
	}
	
	.wrap {
		padding: 30rpx;
	}
</style>
